﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using X.Web;
using System.Text;
using X.Entity;

namespace X.WebSite
{
    public partial class SiteMap : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Response.Clear();
                Response.ContentType = "text/xml";
                string urlbase = Bll.Params.GetSiteInfo().SiteUrl;
                string lastmod = DateTime.UtcNow.ToString("yyyy-MM-dd");
                StringBuilder strBuilder = new StringBuilder();

                strBuilder.AppendLine("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
                strBuilder.AppendLine("<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">");

                //首页
                strBuilder.AppendFormat("<url><loc>{0}</loc><lastmod>{1}</lastmod><changefreq>always</changefreq><priority>1.0</priority></url>", urlbase, lastmod);

                //类别
                IList<ProductCategoryInfo> lstCategory = Bll.ProductCategory.GetCategoryList();
                foreach (ProductCategoryInfo categoryInfo in lstCategory)
                {
                    strBuilder.AppendFormat("<url><loc>{0}/Product/{1}.aspx</loc><lastmod>{2}</lastmod><changefreq>always</changefreq><priority>0.9</priority></url>", urlbase, X.Common.StringUtils.StringToUrl(categoryInfo.CategoryName, categoryInfo.CategoryId), lastmod);

                    //搜索
                    strBuilder.AppendFormat("<url><loc>{0}/Search.aspx?keyword={1}</loc><lastmod>{2}</lastmod><changefreq>always</changefreq><priority>0.7</priority></url>", urlbase, X.Common.StringUtils.HtmlDecode(categoryInfo.CategoryName), lastmod);
                }

                //产品
                IList<ProductInfo> lstProduct = Bll.Product.GetList();
                foreach (ProductInfo productInfo in lstProduct)
                {
                    strBuilder.AppendFormat("<url><loc>{0}/Product/{1}/{2}.aspx</loc><lastmod>{3}</lastmod><changefreq>always</changefreq><priority>0.8</priority></url>", urlbase, X.Common.StringUtils.StringToUrl(productInfo.CategoryName, productInfo.CategoryId), X.Common.StringUtils.StringToUrl(productInfo.ProductName, productInfo.ProductId), lastmod);
                }

                //新闻
                IList<NewsInfo> lstNews = Bll.News.GetAllList();
                foreach (NewsInfo newsInfo in lstNews)
                {
                    strBuilder.AppendFormat("<url><loc>{0}/News/{1}.aspx</loc><lastmod>{2}</lastmod><changefreq>always</changefreq><priority>0.6</priority></url>", urlbase, newsInfo.Id, lastmod);
                }

                //比较
                IList<ProductInfo> lstProduct2 = Bll.Product.GetList();

                foreach (ProductInfo productInfo in lstProduct)
                {
                    foreach (ProductInfo productInfo2 in lstProduct2)
                    {
                        if (productInfo.ProductId != productInfo2.ProductId)
                        {
                            strBuilder.AppendFormat("<url><loc>{0}/Product/{1}_vs_{2}.aspx</loc><lastmod>{3}</lastmod><changefreq>always</changefreq><priority>0.8</priority></url>", urlbase, X.Common.StringUtils.StringToUrl(productInfo.ProductName, productInfo.ProductId), X.Common.StringUtils.StringToUrl(productInfo2.ProductName, productInfo2.ProductId), lastmod);
                        }
                    }
                }

                strBuilder.AppendFormat("<url><loc>{0}/Tag/</loc><lastmod>{1}</lastmod><changefreq>always</changefreq><priority>0.7</priority></url>", urlbase, lastmod);

                strBuilder.Append("</urlset>");

                Response.Write(strBuilder.ToString());
            }
        }
    }
}